/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.accounting.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.javaeye.accounting.dto.MaterialsMonthBatchDetail;
import com.javaeye.accounting.dto.MaterialsMonthInventoryCheck;
import com.javaeye.accounting.dto.MaterialsMonthInventoryCheckDetail;
import com.javaeye.accounting.service.IMaterialsMonthInventoryCheckService;
import com.javaeye.common.util.DateUtils;
import com.javaeye.common.util.NumberUtils;
import com.javaeye.store.dto.MaterialsCategory;
import com.javaeye.store.service.IMaterialsCategoryService;
import com.opensymphony.xwork2.ActionSupport;

public class MaterialsMonthInventoryCheckAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -197381205794811886L;
	
	
	/**
	 * 用于接受查询画面上选择日期的值
	 */
	private String queryDate;
	
	/**
	 * 用于接受查询画面上选择分类的下拉框的值
	 */
	private int rootCategory;
	
	/**
	 * 用于生成查询画面上选择分类的下拉框的值
	 */
	private List<MaterialsCategory> rootCategoryList;
	
	/**
	 * 盘点表ID
	 */
	private String checkNo;
	
	/**
	 * 盘点表
	 */
	private MaterialsMonthInventoryCheck check;
	
	/**
	 * 选择的分类
	 */
	private List<Integer> selectCategoryIds;
	
	/**
	 * 选择的分类
	 */
	private List<MaterialsCategory> selectCategorys;
	
	/**
	 * 查询结果列表
	 */
	private List<MaterialsMonthInventoryCheck> inventoryChecks;
	
	/**
	 * 用于生产盘点表的结果列表
	 */
	private List<MaterialsMonthBatchDetail> checkDetails;
	
	/**
	 * 用于保存实际盘点数量
	 */
	private List<Float> realNumbers;
	
	/**
	 * 修改的明细ID
	 */
	private List<Integer> detailIds;
	
	private IMaterialsMonthInventoryCheckService service;
	
	private IMaterialsCategoryService categoryService;
	
	public void setService(IMaterialsMonthInventoryCheckService service) {
		this.service = service;
	}
	
	public void setCategoryService(IMaterialsCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public MaterialsMonthInventoryCheckAction() {
		rootCategory = -1;
		queryDate = DateUtils.dateToString("yyyy");
	}
	
	/**
	 * 查询年度所有盘点表一览
	 * @return
	 * @throws Exception
	 */
	public String queryMaterialsMonthInventoryCheckList() throws Exception {
		rootCategoryList = categoryService.getCategorys(0);
		inventoryChecks = service.getInventoryChecks(queryDate, rootCategory);
		return SUCCESS;
	}
	
	/**
	 * 初始化用于添加新的盘点计划
	 * @return
	 * @throws Exception
	 */
	public String queryForAddCheck() throws Exception {
		queryDate = DateUtils.getCurrentMonthNo();
		rootCategoryList = categoryService.getCategorys(0);
		return SUCCESS;
	}
	
	/**
	 * 查询物料库存，用于生成盘点计划
	 * @return
	 * @throws Exception
	 */
	public String queryMaterialsInventoryInfo() throws Exception {
		rootCategoryList = categoryService.getCategorys(0);
		if (selectCategoryIds != null) {
			selectCategorys = new ArrayList<MaterialsCategory>();
			for (int id : selectCategoryIds) {
				selectCategorys.add(categoryService.getCategory(id));
			}
			checkDetails = service.queryCheckDetails(queryDate, selectCategoryIds);
		}
		return SUCCESS;
	}
	
	/**
	 * 查询盘点计划信息
	 * @return
	 * @throws Exception
	 */
	public String queryInventoryCheckInfo() throws Exception {
		check = service.getInventoryCheckInfo(checkNo);
		Set<MaterialsMonthInventoryCheckDetail> details = 
			check.getDetails();
		float diffNumber = 0;
		for (MaterialsMonthInventoryCheckDetail detail : details) {
			diffNumber = detail.getRealNumber() - detail.getNumber();
			detail.setDiffNumber((float)NumberUtils.formatDouble(diffNumber));
			detail.setDiffAmount(NumberUtils.formatDouble(diffNumber * detail.getPrice()));
		}
		return SUCCESS;
	}
	
	/**
	 * 保存盘点计划
	 * @return
	 * @throws Exception
	 */
	public String saveInventoryCheckInfo() throws Exception {
		service.saveAddCheckDetails(queryDate, selectCategoryIds);
		return SUCCESS;
	}
	
	/**
	 * 保存实际的盘点结果
	 * @return
	 * @throws Exception
	 */
	public String saveInventoryCheckResult() throws Exception {
		MaterialsMonthInventoryCheck dbCheck = service.getInventoryCheckInfo(checkNo);
		Set<MaterialsMonthInventoryCheckDetail> details = dbCheck.getDetails();
		double realAmount = 0;
		for (int i = 0; i < detailIds.size(); i++) {
			int detailId = detailIds.get(i);
			MaterialsMonthInventoryCheckDetail detail = getInventoryCheckDetail(detailId, details);
			if (detail != null) {
				detail.setRealNumber(realNumbers.get(i));
				realAmount += detail.getPrice() * detail.getRealNumber();
			}
		}
		dbCheck.setRealAmount(NumberUtils.formatDouble(realAmount));
		dbCheck.setDiffAmount(NumberUtils.formatDouble(dbCheck.getRealAmount() - dbCheck.getAmount()));
		service.saveInventoryCheckResult(dbCheck);
		return SUCCESS;
	}
	
	/**
	 * 根据实际盘点结果调整实际库存
	 * @return
	 * @throws Exception
	 */
	public String adjustMaterialsInventory() throws Exception {
		MaterialsMonthInventoryCheck dbCheck = service.getInventoryCheckInfo(checkNo);
		service.adjustMaterialsInventory(dbCheck);
		return SUCCESS;
	}
	
	private MaterialsMonthInventoryCheckDetail getInventoryCheckDetail(int detailId, 
			Set<MaterialsMonthInventoryCheckDetail> details) {
		for (MaterialsMonthInventoryCheckDetail detail : details) {
			if (detailId == detail.getId()) {
				return detail;
			}
		}
		return null;
	}
	
	/**
	 * 删除盘点计划
	 * @return
	 * @throws Exception
	 */
	public String removeInventoryCheck() throws Exception {
		service.removeInventoryCheck(checkNo);
		return SUCCESS;
	}

	public String getQueryDate() {
		return queryDate;
	}

	public void setQueryDate(String queryDate) {
		this.queryDate = queryDate;
	}

	public int getRootCategory() {
		return rootCategory;
	}

	public void setRootCategory(int rootCategory) {
		this.rootCategory = rootCategory;
	}

	public List<MaterialsCategory> getRootCategoryList() {
		return rootCategoryList;
	}

	public void setRootCategoryList(List<MaterialsCategory> rootCategoryList) {
		this.rootCategoryList = rootCategoryList;
	}

	public List<MaterialsMonthInventoryCheck> getInventoryChecks() {
		return inventoryChecks;
	}

	public void setInventoryChecks(
			List<MaterialsMonthInventoryCheck> inventoryChecks) {
		this.inventoryChecks = inventoryChecks;
	}

	public MaterialsMonthInventoryCheck getCheck() {
		return check;
	}

	public void setCheck(MaterialsMonthInventoryCheck check) {
		this.check = check;
	}

	public List<MaterialsMonthBatchDetail> getCheckDetails() {
		return checkDetails;
	}

	public void setCheckDetails(
			List<MaterialsMonthBatchDetail> checkDetails) {
		this.checkDetails = checkDetails;
	}

	public List<Integer> getSelectCategoryIds() {
		return selectCategoryIds;
	}

	public void setSelectCategoryIds(List<Integer> selectCategoryIds) {
		this.selectCategoryIds = selectCategoryIds;
	}

	public List<MaterialsCategory> getSelectCategorys() {
		return selectCategorys;
	}

	public void setSelectCategorys(List<MaterialsCategory> selectCategorys) {
		this.selectCategorys = selectCategorys;
	}

	public void setCheckNo(String checkNo) {
		this.checkNo = checkNo;
	}

	public String getCheckNo() {
		return checkNo;
	}

	public List<Float> getRealNumbers() {
		return realNumbers;
	}

	public void setRealNumbers(List<Float> realNumbers) {
		this.realNumbers = realNumbers;
	}

	public List<Integer> getDetailIds() {
		return detailIds;
	}

	public void setDetailIds(List<Integer> detailIds) {
		this.detailIds = detailIds;
	}
	
}
